package br.com.sigde.framework.modelo;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import br.com.sigde.framework.exception.DAOException;
import br.com.sigde.framework.exception.ServiceException;

public class ComumDAO {

	protected BigDecimal getNextKey(String sequence, Connection conn) throws ServiceException,
			DAOException {
		try{
			String sql = "SELECT <SEQUENCE>.NEXTVAL FROM DUAL";
			sql = sql.replaceAll("<SEQUENCE>", sequence);
			PreparedStatement stm = conn.prepareStatement(sql);
			ResultSet rs = stm.executeQuery();
			rs.next();
			BigDecimal nuSequence = rs.getBigDecimal(1);
			close(rs);
			close(stm);
			return nuSequence;
		}catch(Exception e){
			throw new DAOException(e);
		}
	}
	protected void close(PreparedStatement stm) throws DAOException{
			
			try {
				if(stm!=null){
					stm.close();
				}
			} catch (Exception e) {
				throw new DAOException(e);
			}
	}
	protected void close(ResultSet rs) throws DAOException{
		try {
			if(rs!=null){
				rs.close();
			}
		} catch (Exception e) {
			throw new DAOException(e);
		}
	}
}
